const BASE_API = 'http://localhost:8080'
const API_LOGIN = '/user/login'
const API_REGISTER = '/user/register'


function get(url, data, res) {
	request(url, data, res, 'GET')
}

function post(url, data, res) {
	request(url, data, res, 'POST')
}

function request(url, data, res, method) {
	let header = method == 'POST' ? {
		'Content-Type': 'application/json',
		'token': uni.getStorageSync('token')
	} : {
		'token': uni.getStorageSync('token')
	};
	uni.request({
		url: BASE_API + url,
		method: method,
		header: header,
		data: data,
		success: (data) => {
			if (data.data.code === 200) {
				res(data.data)
			} else {
				uni.showToast({
					title: data.data.msg,
					icon: 'none',
					duration: 2000 //时间
				});
			}
		}
	});
}


export default {
	get: function(url, data, res) {
		return get(url, data, res);
	},
	post: function(url, data, res) {
		return post(url, data, res);
	},
	API_LOGIN,
	API_REGISTER
}
